package spring.controller;


import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import mapper.UserMapper;
import mapper.VehicleMapper;
import spring.service.DemoService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import entity.Blog;
import entity.Page;
import entity.User;
import entity.UserInfo;
import entity.Vehicle;


@Controller
@RequestMapping("/demo")
public class DemoController {

	
	private UserMapper userMapper;
	private VehicleMapper vehicleMapper;
	
	@Autowired
	private DemoService demoService;
	
	
	
	private Logger logger = Logger.getLogger(this.getClass());

	private DataSourceTransactionManager tm;
	
	@Resource
	public void setUserMapper(UserMapper userMapper) {
		this.userMapper = userMapper;
	}
	
	@Resource
	public void setVehicleMapper(VehicleMapper vehicleMapper){
		  this.vehicleMapper = vehicleMapper;
	}
	
	@Autowired
	@Qualifier("txManager")
	public void setTm(DataSourceTransactionManager tm) {
		this.tm = tm;
	}
	
	
	@RequestMapping(value="/test",produces="text/html;charset=utf-8")
	public @ResponseBody String test(Date date){
		
		
		
		return "我de网站";
	}
	
	@RequestMapping("testJsp")
	public String testJsp(User user){
		
		logger.info(user);
		
		return "test";
	}

	
	@RequestMapping(value="/insert",produces="text/html;charset=utf-8")
	//@Transactional(rollbackFor=ClassNotFoundException.class)
	@Transactional
	public @ResponseBody String insert(HttpServletRequest r) {
		User user = new User();
		user.setEmail("223@qq.com");
		user.setPassword("123");
		user.setNickname("weck");
		UserInfo info = new UserInfo();
		info.setUser(user);
		info.setAge(16);
		info.setSex("男");
		userMapper.saveUser(user);
		userMapper.saveUserInfo(info);
		return "ok";
	}
	

	public @ResponseBody String insert2(){
		 TransactionTemplate tran = 
		            new TransactionTemplate(tm);
		 tran.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus tran) {
				Object obj = tran.createSavepoint();
				tran.releaseSavepoint(obj);
				tran.rollbackToSavepoint(obj);
			}
		 });
		
		return "insert2";
	}
	
	@RequestMapping(value="/page",produces="text/html;charset=utf-8")
	public @ResponseBody String page(){
		List<UserInfo> list =  
				userMapper.getUserInfoList(
						new Page(1, 3, "user_info_id desc")); 
		StringBuilder res = new StringBuilder();
		if(list == null) {
				return "null";
		}
		for(UserInfo info : list){
			res.append(info.toString());
			res.append("\r\n");
		}
		return res.toString();
	}
	
	
	@RequestMapping(value="/json",produces="application/json;charset=utf-8")
	public @ResponseBody boolean json(){
		
		return true;
	}
	
	@RequestMapping(value="/getUser",produces="text/html;charset=utf-8")
	public @ResponseBody String getUser(){
		User u = userMapper.getUser(1);
		return u.toString();
	}
	@RequestMapping(value="/getUserInfo2",produces="text/html;charset=utf-8")
	public @ResponseBody String getUserInfo2(){
		UserInfo u = userMapper.getUserInfo2(1);
		return u.toString();
	}
	
	@RequestMapping(value="/insertBlog",produces="text/html;charset=utf-8")
	public @ResponseBody String insertBlog(){
		User user = userMapper.getUser(2);
		for(int i=1;i<=10;i++){
			Blog blog = new Blog();
			blog.setContent("今天是风和日丽的第"+i+"天");
			blog.setTitle("日志"+i);
			blog.setUser(user);
			userMapper.saveBlog(blog);
		}
		return "ok";
	}
	
	
	
	@RequestMapping(value="/getUserWithBlog",produces="text/html;charset=utf-8")
	public @ResponseBody String getUserWithBlog(){
		User u = userMapper.getUseWithBlogById(2);
		return u.getBlogs().toString();
	}
	

	@RequestMapping(value="/selectVehicle",produces="text/html;charset=utf-8")
	public @ResponseBody String selectVehicle(){
		Vehicle v = vehicleMapper.selectVehicle(2);
		return v.toString();
	}
	
	@RequestMapping(value="/rest/{id}",method=RequestMethod.GET)
	public @ResponseBody  String rest(@PathVariable("id") int i){
		
		logger.info(i);
		
		return "rest";
	}
	
	@RequestMapping("index")
	public String index(Model model){
		model.addAttribute("msg", "hello");
		return "index";
	}
	
	@RequestMapping("/addUser")
	public @ResponseBody String addUser(User user,UserInfo info){
		
		//logger.info(user);
		if(user == null)
			return "user is null";
		if(info == null)
			return "info is null";
		return user.toString()+"|"+info.toString();
	}
	
	@RequestMapping("/register.html")
	public String register(){
		return "register";
	}
	
	
}
